package com.nulldev.util.threading.v3;

import java.util.ArrayList;
import java.util.List;

public class ThreadsMonitor implements IWorker {
	private List<IWorker> iWorkers = new ArrayList<IWorker>();
    public static ThreadsMonitor threadsMonitor;
    private boolean work;

    private ThreadsMonitor() {}

    @Override
    public boolean isAlive() {
        return Thread.currentThread().isAlive();
    }

    @Override
    public String getName() {
        return Thread.currentThread().getName();
    }

    @Override
    public int getID() {
        return (int) Thread.currentThread().getId();
    }

    @Override
    public void shutdown() {
    	this.work = false;
    }

    @Override
    public void run() {
    	this.work = true;
        while (this.work) {
            for (IWorker iWorker : iWorkers) {
                System.out.println(iWorker.getName() + " " + iWorker.getID());
            }
        }
    }

    public void addIWorker(final IWorker iWorker) {
    	this.iWorkers.add(iWorker);
    }

    public static ThreadsMonitor instance() {
        if (threadsMonitor == null)
            threadsMonitor = new ThreadsMonitor();
        return threadsMonitor;
    }
}
